1902C - Insert and Equalize - CodeForces Solution


brute force constructive algorithms greedy math number theory shortest paths

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back
int main()
{
int tc;
cin>>tc;
while(tc--){
  ll n;cin>>n;
  vll v(n);
  ll sum=0;
  f(i,0,n){
    cin>>v[i];    
  }
  if(n==1){
    cout<<"1\n";
    continue;
  }
  sort(v.begin(),v.end());
  ll gcd=0,ans=0;
  for(ll i=0;i<n-1;++i){
    gcd=__gcd(gcd,v[n-1]-v[i]);
  }
  for(ll i=0;i<n;++i){
    sum+=(v[n-1]-v[i])/gcd;
  }
  ans=v[n-1]-gcd;
  ll cnt=1;
  for(int i=n-2;i>=0;--i){
    if(ans!=v[i]){
      break;
    }
      cnt++;
      ans-=gcd;
  }
  cout<<sum+cnt<<"\n";
}
return 0;
}


Comments

Submit
0 Comments
More Questions

1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke